package com.hymall.controller.admin;

import com.hymall.anno.AddLog;
import com.hymall.bean.MarketOrder;
import com.hymall.bean.MarketOrderGoods;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.BaseParam;
import com.hymall.bean.common.BaseRespVo;
import com.hymall.bean.common.CommonData;
import com.hymall.bean.vo.AdminOrderChannelVo;
import com.hymall.bean.vo.AdminOrderUserVo;
import com.hymall.mapper.MarketOrderMapper;
import com.hymall.service.admin.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: 牟治宇
 * @description 商场管理-->订单管理
 * @date: 2022/7/18 10:55
 */


@RestController
@RequestMapping("/admin/order")
public class AdminOrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    MarketOrderMapper marketOrderMapper;


    /**
     * @param [basePageInfo]
     * @return com.hymall.bean.common.BaseRespVo
     * @author 牟治宇
     * @description 响应请求的分页方法
     * @date 2022/7/18 11:07
     */
    @GetMapping("list")
    public BaseRespVo getOrderList(@DateTimeFormat(
            pattern = "yyyy-MM-dd HH:mm:ss") Date start, @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date end,
                                   Short[] orderStatusArray, String userId, String orderId, String orderSn,
                                   BasePageInfo basePageInfo) {


        Integer id = null;
        if (userId != null && !"".equals(userId)) {
            id = Integer.parseInt(userId);
        }

        Integer oid = null;
        if (orderId != null && !"".equals(orderId)) {

            oid = Integer.parseInt(orderId);
        }

        CommonData data = orderService.getOrderList(start, end, orderStatusArray, id, oid, orderSn, basePageInfo);
        return BaseRespVo.ok(data);
    }

    /**
     * @param []
     * @return com.hymall.bean.common.BaseRespVo
     * @author 牟治宇
     * @description 删除订单--不能删除订单
     * @date 2022/7/21 14:17
     */
    @PostMapping("delete")
    public BaseRespVo deleteOrder() {
        return BaseRespVo.error(623, "不能删除订单");
    }


    /**
     * @param [id]
     * @return com.hymall.bean.common.BaseRespVo
     * @author 牟治宇
     * @description 查询订单详情的方法
     * @date 2022/7/18 16:05
     */
    @GetMapping("detail")
    public BaseRespVo orderDetail(Integer id) {
        Map<String, Object> map = orderService.queryOrderDetail(id);
        return BaseRespVo.ok(map);
    }



    /**
     * @param [map]
     * @return com.hymall.bean.common.BaseRespVo
     * @author 牟治宇
     * @description 发货状态修改
     * @date 2022/7/18 16:06
     */
    @AddLog(action = "发货", type = 2)
    @PostMapping("ship")
    public BaseRespVo orderShip(@RequestBody Map<String, Object> map) {
        Integer orderId = (Integer) map.get("orderId");
        String shipChannel = (String) map.get("shipChannel");
        String shipSn = (String) map.get("shipSn");
        int code = marketOrderMapper.updateShip(orderId, shipChannel, shipSn);
        if (code != 1) {
            return BaseRespVo.error(1, "error");
        }
        return BaseRespVo.ok();
    }


    /**
     * @param [map]
     * @return com.hymall.bean.common.BaseRespVo
     * @author 牟治宇
     * @description 退款状态
     * @date 2022/7/18 16:06
     */
    @AddLog(action = "退款", type = 2)
    @PostMapping("refund")
    public BaseRespVo orderRefund(@RequestBody Map<String, Object> map) {
        Integer orderId = (Integer) map.get("orderId");
        double money = Double.parseDouble(map.get("refundMoney").toString());
        BigDecimal refundMoney = new BigDecimal(money);
        int code = orderService.orderRefund(refundMoney, orderId);
        if (code != 1) {
            return BaseRespVo.error(1, "error");
        }
        return BaseRespVo.ok();
    }

    @GetMapping("channel")
    public BaseRespVo orderChannel() {

        List<AdminOrderChannelVo> channelVo = orderService.getChannel();
        return BaseRespVo.ok(channelVo);
    }

    @PostMapping("reply")
    public BaseRespVo replyOrder(@RequestBody Map<String,String> map){
        String commentId = map.get("commentId");
        String content = map.get("content");

        int code = orderService.insertComment(commentId,content);
        if (code == -1){
            return BaseRespVo.error(622,"已回复");
        }
        return BaseRespVo.ok(null);
    }
}
